package com.testdocs.client.version.list;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.testdocs.client.ClientFactory;
import com.testdocs.client.project.ProjectInfo;
import com.testdocs.client.project.ProjectService;
import com.testdocs.client.project.ProjectServiceAsync;
import com.testdocs.client.project.event.ProjectSelectedEvent;
import com.testdocs.client.project.i18n.ProjectConstants;
import com.testdocs.client.validation.ConstraintViolationException;
import com.testdocs.client.version.VersionInfo;
import com.testdocs.client.version.VersionService;
import com.testdocs.client.version.VersionServiceAsync;
import com.testdocs.client.version.i18n.VersionConstants;

public class VersionListActivity extends AbstractActivity implements
		VersionListView.Presenter {
	String projectId;
	ProjectInfo project;
	ClientFactory clientFactory;
	
	VersionConstants versionConstants = (VersionConstants) GWT.create(VersionConstants.class);

	public VersionListActivity(VersionListPlace place,
			ClientFactory clientFactory) {
		this.projectId = place.getProjectId();
		this.clientFactory = clientFactory;
		clientFactory.getVersionListView().setPresenter(this);
	}

	@Override
	public void start(final AcceptsOneWidget panel, EventBus eventBus) {
		final VersionListView view = clientFactory.getVersionListView();
		loadProjectInfo(this.projectId);

		clientFactory.getStatusBar().setStatus(versionConstants.loadingVersionInfoStatus());
		VersionServiceAsync versionService = GWT.create(VersionService.class);		
		versionService.getVersions(projectId, new AsyncCallback<VersionInfo[]>() {
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getRpcErrorHandler().handle(caught);
			}

			@Override
			public void onSuccess(VersionInfo[] result) {
				clientFactory.getStatusBar().clear();
				view.setVersionsList(result);
				panel.setWidget(view);
			}
		});
	}
	
	private void loadProjectInfo(String roleId) {
		ProjectConstants projectConstants = (ProjectConstants) GWT.create(ProjectConstants.class);
		
		ProjectServiceAsync projectService = GWT.create(ProjectService.class);
		if (clientFactory.getProjectContextHolder().getProject() == null) {
			clientFactory.getWaitingPopupPanel().show(projectConstants.loadingProjectInfoStatus());
			
			projectService.getProjectInfo(roleId, new AsyncCallback<ProjectInfo>() {
				@Override
				public void onFailure(Throwable caught) {
					clientFactory.getWaitingPopupPanel().hide();
					clientFactory.getRpcErrorHandler().handle(caught);
				}
				@Override
				public void onSuccess(ProjectInfo result) {
					clientFactory.getEventBus().fireEvent(new ProjectSelectedEvent(result));
					clientFactory.getWaitingPopupPanel().hide();
					project = result;
				}
			});
		}
	}

	@Override
	public void saveNewVersion(final VersionInfo newVersion, String basedOnVersionId) throws ConstraintViolationException {
		clientFactory.getValidator().validate(newVersion);

		VersionServiceAsync versionService = GWT.create(VersionService.class);
		clientFactory.getStatusBar().setStatus(versionConstants.savingStatus());
		versionService.createVersion(newVersion, basedOnVersionId, new AsyncCallback<VersionInfo>(){
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getRpcErrorHandler().handle(caught);
			}
			@Override
			public void onSuccess(VersionInfo savedVersionInfo) {
				clientFactory.getStatusBar().clear();
				clientFactory.getVersionListView().setSavedVersion(newVersion, savedVersionInfo);
			}
		});
	}

	@Override
	public void remove(VersionInfo versions[]) {
		VersionServiceAsync versionService = GWT.create(VersionService.class);
		clientFactory.getStatusBar().setStatus(versionConstants.savingStatus());
		versionService.remove(versions, new AsyncCallback<Void>(){
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getRpcErrorHandler().handle(caught);
			}
			@Override
			public void onSuccess(Void o) {
				clientFactory.getStatusBar().clear();
			}
		});		
	}

	@Override
	public void rename(VersionInfo version) throws ConstraintViolationException {
		clientFactory.getValidator().validate(version);

		VersionServiceAsync versionService = GWT.create(VersionService.class);
		clientFactory.getStatusBar().setStatus(versionConstants.savingStatus());
		versionService.rename(version, new AsyncCallback<Void>(){
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getRpcErrorHandler().handle(caught);
			}
			@Override
			public void onSuccess(Void o) {
				clientFactory.getStatusBar().clear();
			}
		});				
	}
}
